package com.zhou.bjgl.bjgl.business_web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhou.bjgl.bjgl.business_web.entity.BNotice;
import com.zhou.bjgl.bjgl.business_web.entity.BNoticeRead;
import com.zhou.bjgl.bjgl.business_web.mapper.BNoticeMapper;
import com.zhou.bjgl.bjgl.business_web.mapper.BNoticeReadMapper;
import com.zhou.bjgl.bjgl.business_web.service.BNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.req.b_notice.BNoticeAddReq;
import com.zhou.bjgl.bjgl.req.b_notice.BNoticeQueryReq;
import com.zhou.bjgl.bjgl.req.b_notice.BNoticeReadReq;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.resp.notice.BNoticeResp;
import com.zhou.bjgl.bjgl.resp.notice.NoticeNotReadResp;
import com.zhou.bjgl.bjgl.util.CopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  通知servcie实现类
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@Service
public class BNoticeServiceImpl extends ServiceImpl<BNoticeMapper, BNotice> implements BNoticeService {

    @Resource
    private BNoticeMapper bNoticeMapper;

    @Resource
    private BNoticeReadMapper bNoticeReadMapper;

    /**
     * 获取通知列表
     * @param req
     * @return
     */
    @Override
    public PageResp<BNoticeResp> listNotice(BNoticeQueryReq req) {
        PageHelper.startPage(req.getPage(),req.getSize());
        List<BNoticeResp> bNoticeRespList=bNoticeMapper.QueryNoticeList(req);

        PageResp<BNoticeResp> bNoticeRespPageResp = new PageResp<>();
        PageInfo<BNoticeResp> bNoticeRespPageInfo = new PageInfo<>(bNoticeRespList);
        bNoticeRespPageResp.setPageList(bNoticeRespList);
        bNoticeRespPageResp.setTotal(bNoticeRespPageInfo.getTotal());
        return bNoticeRespPageResp;
    }

    @Override
    public void AddNotice(BNoticeAddReq req) {
        BNotice bNotice = CopyUtil.copy(req, BNotice.class);
        bNoticeMapper.insert(bNotice);
    }

    @Override
    public BNoticeResp queryNoticeDetail(BNoticeQueryReq req) {
        BNoticeResp bNoticeResp=bNoticeMapper.queryNoticeDetail(req);
        return bNoticeResp;
    }

    @Override
    public void markNoticeRead(BNoticeRead bNoticeRead) {
        QueryWrapper<BNoticeRead> bNoticeReadQueryWrapper = new QueryWrapper<>();
        bNoticeReadQueryWrapper.eq("notice_id",bNoticeRead.getNoticeId());
        BNoticeRead bNoticeReadOld = bNoticeReadMapper.selectOne(bNoticeReadQueryWrapper);

        if (bNoticeReadOld == null) {
            bNoticeReadMapper.insert(bNoticeRead);
        }
    }

    @Override
    public PageResp<NoticeNotReadResp>  queryNoticeNotRead(BNoticeReadReq bNoticeReadReq) {

        PageHelper.startPage(bNoticeReadReq.getPage(),bNoticeReadReq.getSize());
        List<NoticeNotReadResp> noticeNotReadResps=bNoticeReadMapper.queryNoticeNotRead(bNoticeReadReq);

        PageInfo<NoticeNotReadResp> noticeNotReadRespPageInfo = new PageInfo<>(noticeNotReadResps);

        PageResp<NoticeNotReadResp> notReadRespPageResp = new PageResp<>();
        notReadRespPageResp.setTotal(noticeNotReadRespPageInfo.getTotal());
        notReadRespPageResp.setPageList(noticeNotReadResps);

        return notReadRespPageResp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editNotice(BNoticeAddReq req) {
        BNotice bNotice = new BNotice();
        bNotice.setEditFlag(1);
        //判断id是否为空
        if (!ObjectUtils.isEmpty(req.getId())) {
            bNotice.setId(req.getId());
        }else {
            throw new BwException(BwExceptionEnum.ID_IS_NULL);
        }
        //注入内容
        if (!ObjectUtils.isEmpty(req.getMsg())) {
            bNotice.setMsg(req.getMsg());
        }
        //注入主题
        if (!ObjectUtils.isEmpty(req.getTheme())) {
            bNotice.setTheme(req.getTheme());
        }
        bNoticeMapper.updateById(bNotice);

        //修改成功之后删除已读
        QueryWrapper<BNoticeRead> bNoticeReadQueryWrapper = new QueryWrapper<>();
        bNoticeReadQueryWrapper.eq("notice_id",req.getId());
        bNoticeReadMapper.delete(bNoticeReadQueryWrapper);
    }
}
